package progressBar.use;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import javax.swing.border.TitledBorder;

public class ProgressBarFrame {

	// field declaration
	private static JTextArea textArea;
	private static JButton btnStart;
	private static JProgressBar progressBar;
	private static JScrollPane scrollPane;
	private static final int MAX = 1000;
	private static Task t;

	public static void main(String[] args) {
		Runnable runner = new Runnable() {

			@Override
			public void run() {
				JFrame.setDefaultLookAndFeelDecorated(true);
				JFrame frame = new JFrame("Use Progress Bar");
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

				progressBar = new JProgressBar(0, MAX);
				progressBar.setStringPainted(true);

				JPanel contentPane = (JPanel) frame.getContentPane();
				TitledBorder border =
					new TitledBorder(null, "Progress Results",
							TitledBorder.TOP, TitledBorder.CENTER, new Font(
									"San-Serif", Font.BOLD | Font.ITALIC, 15),
							Color.RED);
				contentPane.setBorder(border);

				textArea = new JTextArea();
				scrollPane =
					new JScrollPane(textArea,
							JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
				contentPane.add(scrollPane, BorderLayout.CENTER);

				btnStart = new JButton("Start");
				ActionListener action = new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						btnStart.setEnabled(false);
						t = new Task(MAX);
						t.execute();
					}
				};
				btnStart.addActionListener(action);

				JPanel south = new JPanel();
				south.add(btnStart, BorderLayout.WEST);
				south.add(progressBar, BorderLayout.CENTER);
				contentPane.add(south, BorderLayout.SOUTH);

				frame.setLocationRelativeTo(null);
				frame.setResizable(false);
				frame.setSize(400, 300);
				frame.setVisible(true);
			}
		}; // end runnable

		EventQueue.invokeLater(runner);
	}

	static class Task extends SwingWorker<Void, Integer> {

		private int target;
		private int current;

		public Task(int i) {
			current = 0;
			target = i;
		}

		@Override
		protected Void doInBackground() throws Exception {

			while (current < target) {
				Thread.sleep(20);
				current++;
				publish(current);
			}
			return null;
		}

		@Override
		protected void process(List<Integer> chunks) {
			for (Integer integer : chunks) {
				progressBar.setValue(integer);
				textArea.append(integer + " \n");
			}
		}

		@Override
		protected void done() {
			btnStart.setEnabled(true);
		}
	}
}
